<?php

namespace app\api\validate;

use think\Validate;
class Posts extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
	    'id'=>'require|number',
	    'airport_id'=>'require|number',
	    'title'=>'require|max:50',
        'sub_title'=>'max:50',
        'content'=>'max:500',
        'start_time'=>'compare_time',
        'end_time'=>'compare_time',
        'type'=>'require|in:1,2',
        'source_type'=>'in:1,2',

    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'id.require'=>'ID cannot be empty',
        'id.number'=>'ID must be a number',
        'title.require'=>'Title cannot be empty',
        'title.max'=>'Title cannot exceed 50 characters in length',
        'sub_title.max'=>'Title cannot exceed 50 characters in length',
//        'content.require'=>'Content cannot be empty',
        'content.max'=>'Content cannot exceed 500 characters in length',
        'source_type.in'=>'source type must 1 or 2',
        'airport_id.require'=>'Airport ID cannot be empty',
        'airport_id.number'=>'Airport ID must be a number',
    ];

    protected $scene=[
        'add'=>['title','sub_title','content','start_time','end_time'],
        'edit'=>['id','title','sub_title','content','start_time','end_time'],
        'delete'=>['id'],
        'info'=>['id'],
        'like'=>['id'],
        'complete'=>['id','country','airport','terminal','category','phone_number','telephone'],
        'register'=>['name','email','password','repassword','phone'],
        'lists'=>['id'],
    ];


    /**
     * 验证时间
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool|string
     */
    protected function compare_time($value,$rule,$data=[])
    {
        $is_start_time=(isset($data['start_time']) && $data['start_time'])?1:0;
        $is_end_time=(isset($data['end_time']) && $data['end_time'])?1:0;
        if($is_end_time ^ $is_start_time) return 'Start time and end time must exist simultaneously';
        if(strtotime($data['start_time'])>=strtotime($data['end_time'])) return 'The start time cannot be greater than the end time';
        return true;
    }



    public function sceneLogin_2(){
        return $this->only(['phone','password','type'])
            ->remove('password','compare')
            ->append('password', 'require');
    }
}
